function filterMessage(friend, messages, type) {
    var retMessages = [];
    if( type == 'ims' ) { 
        for( var i = 0, len = messages.length; i < len; i++ ) {
            var message = messages[i];
            if( message && ((message.uid in friend.shield) || (message.uid in friend.black)) ) {
                continue;
            }

            retMessages.push(message);
        }
    }else if( type == 'message' ) {
        for( var i = 0, len = messages.length; i < len; i++ ) {
            var message = messages[i];
            if( message && (message.type == 'chat') && 
                ((message.uid in friend.shield) || (message.uid in friend.black)) ){ 
                continue;
            }

            retMessages.push(message);
        }
    }

    return retMessages;
}

exports.get = function(player, req, resp, onHandled) {
    resp.nolog = 1;
    var user = player.user;

    req.args.league = user.league.name;
    requestWorld(req, resp, function(){
        if( resp.code == 0 && resp.data.prison ) {
            // 监狱抓捕
            user.prison.enemy = resp.data.prison;
            player.markDirty('prison.enemy');
        }

        if( player.cloudMessages.length > 0 ) {
            resp.data.cloud = player.cloudMessages;
            player.cloudMessages = [];
        }

        // 好友消息过滤,滤除玩家屏蔽和黑名单中的好友消息
        var friend = gFriend.getUser(req.uid);;
        resp.data.messages = filterMessage(friend, resp.data.messages, 'message');
        resp.data.ims = filterMessage(friend, resp.data.ims, 'ims');

        onHandled();
    });
    
    if( user.status.level < gConfGlobal.ServerMineOpenLevel ) {
        return;
    }

    // 获取跨服金矿云服务器消息
    var cloudReq = {
        uid: req.uid,
        mod: 'servermine',
        act: 'message',
        args: {
        }
    };
    var cloudResp = {};
    requestServerMine(cloudReq, cloudResp, function(){
        if( cloudResp.code == 0 ) {
            player.cloudMessages = cloudResp.data.messages;
        }
    });
}

exports.headline = function(player, req, resp, onHandled) {
    resp.nolog = 1;
    
    if (player.user.status.level < gConfGlobal.TinyTrumpetLimit) {
        resp.code = 1;
        resp.desc = 'level limit';
        onHandled();
        return;
    }

    if( !verifyWordFilter(req.args.content, req.args.key) || player.user.activity.block_talk) {
        resp.code = 1;
        resp.desc = 'verify fail';
        onHandled();
    }else if( player.getInventory('material', 76) <= 0 
        && !player.addInventory('material', 39, -1) ) {
    
        resp.code = 1;
        resp.desc = 'not enough material';
        onHandled();
    }else{
        req.args.gentry = +(player.getInventory('material', 76) > 0);
        requestWorld(req, resp, onHandled);
    }
}

exports.im = function(player, req, resp, onHandled) {
    resp.nolog = 1;

    if( player.user.status.level < gConfGlobal.TinyTrumpetLimit) {
        resp.code = 1;
        resp.desc = 'level limit';
        onHandled();
        return;
    }

    if( !verifyWordFilter(req.args.content, req.args.key) || player.user.activity.block_talk) {
        resp.code = 1;
        resp.desc = 'verify fail';
        onHandled();
    }else if( player.getInventory('material', 40) <= 0 
        && player.getInventory('material', 76) <= 0 
        && !player.addInventory('material', 38, -1) ) {

        resp.code = 1;
        resp.desc = 'not enough material';
        onHandled();
    }else{
        req.args.gentry = +(player.getInventory('material', 76) > 0);
        requestWorld(req, resp, onHandled);
    }
}

exports.leaguetalk = function(player, req, resp, onHandled) {
    resp.nolog = 1;
    if( !verifyWordFilter(req.args.content, req.args.key) ) {
        resp.code = 1;
        resp.desc = 'verify fail';
        onHandled();
    }else{
        req.args.league = player.user.league.name;
        req.args.gentry = +(player.getInventory('material', 76) > 0);
        requestWorld(req, resp, onHandled);
    }
}

exports.chat = function(player, req, resp, onHandled) {
    resp.nolog = 1;
    if( !verifyWordFilter(req.args.content, req.args.key) || player.user.activity.block_talk) {
        resp.code = 1;
        resp.desc = 'verify fail';
        onHandled();
    }else if( player.getInventory('material', 40) <= 0 
        && player.getInventory('material', 76) <= 0 
        && !player.addInventory('material', 38, -1) ) {

        resp.code = 1;
        resp.desc = 'not enough material';
        onHandled();
    }else{
        req.args.gentry = +(player.getInventory('material', 76) > 0);
        requestWorld(req, resp, onHandled);
    }
}

exports.champsbattle_chat = function(player, req, resp, onHandled) {
    resp.nolog = 1;
    if (!verifyWordFilter(req.args.content, req.args.key) || player.user.activity.block_talk) {
        resp.code = 1; resp.desc = 'verify fail';
        onHandled();
    } else if (!player.addInventory('material', 38, -1)) {
        resp.code = 1; resp.desc = 'not enough material';
        onHandled();
    } else {
        req.args.gentry = +(player.getInventory('material', 76) > 0);
        requestWorld(req, resp, onHandled);
    }
}
